import _ from 'lodash';
// import {MessageBox} from 'element-ui';

const pageSizes = [10, 20, 30, 40, 50, 60];
const menus = {
  首页: 'Main',
  咨询: 'Consult',
  常见病咨询: 'CommonDisease',
  保健咨询: 'HealthCare',
  评测: 'Evaluation',
  会员: 'Member',
  新增会员: 'MemberNew',
  宝宝列表: 'MemberList',
  电子病历: 'MemberArchive',
  项目: 'Project',
  添加项目: 'ProjectNew',
  项目管理: 'ProjectManage',
  门店: 'Shop'
};

const user = {};

const menusNavigation = {
  allMenus: menus,
  tabsMenu: [],
  tabText: _.invert(menus),
  activeTab: '0',
  menuActive: 'Main',
  tabs: [
    {
      tab: 'Main',
      text: '首页',
      key: _.uniqueId('Main'),
      isEditing: false
    }
  ],
  navTab(tab, nav, params = {}) {
    //  切换tab页面， 同时是否有标签定位, 页面特有参数需要
    const index = _.findIndex(menusNavigation.tabs, t => t.tab === tab);
    const isOnemenu = _.findIndex(
      menusNavigation.onemenus,
      m => menusNavigation.allMenus[m.name] === tab
    );
    menusNavigation.menuActive = tab;
    if (isOnemenu === -1) {
      menusNavigation.closeMenu();
    }
    if (index !== -1) {
      menusNavigation.activeTab = `${index}`;
      menusNavigation.tabs[index].nav = {
        nav,
        params,
        key: _.uniqueId(menusNavigation.tabs[index].tab)
      };

      return;
    }
    menusNavigation.tabs.push({
      isEditing: false,
      tab,
      text: menusNavigation.tabText[tab],
      nav: {
        nav,
        key: _.uniqueId(tab),
        params
      }
    });
    menusNavigation.activeTab = `${menusNavigation.tabs.length - 1}`;
  },
  closeTab(name) {
    menusNavigation.tabs.splice(+name, 1);
    if (!menusNavigation.tabs[menusNavigation.activeTab]) {
      menusNavigation.activeTab = `${menusNavigation.activeTab - 1}`;
    }

    if (menusNavigation.activeTab === '0') {
      menusNavigation.menuActive = 'Main';
      menusNavigation.closeMenu();
      return;
    }
    menusNavigation.menuActive =
      menusNavigation.tabs[menusNavigation.activeTab].tab;

    const {text} = menusNavigation.tabs[+menusNavigation.activeTab];
    const menu = _.find(menusNavigation.tabsMenu, ({name}) => name === text);
    if (menu && menu.is_url === 1) {
      menusNavigation.closeMenu();
    }
  },
  closeMenu() {
    _.forEach(menusNavigation.tabsMenu, item =>
      menusNavigation.$refsMenu.close(menusNavigation.allMenus[item.name])
    );
  }
};

export {user, menusNavigation, pageSizes};
